/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Nhu
 */
public class Helper {

    private static JDBCHelper con = new JDBCHelper();
    private static DefaultTableModel dt;
    private static ResultSet rs;

    public static void getListCBO(String sql, JComboBox combo, String text, Object value) {
        combo.removeAllItems();

        //combo.insertItemAt("----Select----", 0);
        try {
            rs = con.excuteSelect(sql);
            while (rs.next()) {
                String textField = rs.getString(text);
                Object valueField = rs.getObject(value.toString());
                combo.addItem(textField + "-" + valueField);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }


    }

    public static void getListCombo(String sql, JComboBox combo, String text) {
        combo.removeAllItems();
        combo.insertItemAt("----Select----", 0);
        try {
            rs = con.excuteSelect(sql);
            while (rs.next()) {
                String textField = rs.getString(text);
                combo.addItem(textField);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }


    }

    public static Object getValueTable(JTable table, int index) {
        Object obj = new Object();
        int rows = table.getSelectedRow();
        return obj = table.getValueAt(rows, index);
    }

    public static void inforMessage(String content, String type) {
        JOptionPane.showMessageDialog(null, "<html><font size = '4' style='color:red'><b> "
                + content + "</b></font></html>", type, JOptionPane.WARNING_MESSAGE);
    }

    public static void successMessage(String content, String type) {
        JOptionPane.showMessageDialog(null, "<html><font size = '4' style='color:red'><b> "
                + content + "</b></font></html>", type, JOptionPane.INFORMATION_MESSAGE);
    }

    public static void loadDataTable(String sql, JTable tbl, String[] header) {
        try {
            ResultSet rs = con.excuteSelect(sql);
            ResultSetMetaData metaData = rs.getMetaData();
            int col = metaData.getColumnCount();
            dt = new DefaultTableModel(header, 0);
            while (rs.next()) {
                String[] values = new String[col];
                for (int i = 1; i <= col; i++) {
                    values[i - 1] = rs.getString(i);
                }
                dt.addRow(values);
            }
            tbl.setModel(dt);
        } catch (SQLException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void loadDataTableFromArrayList(List listData, JTable tbl, String[] header) {
        try {
            int col = header.length;
            dt = new DefaultTableModel(header, 0);
            if (listData.size() > 0) {
                String[] values = new String[col];
                for (int i = 0; i >= listData.size(); i++) {
                    values[i - 1] = listData.get(i).toString();
                }
                dt.addRow(values);
            }
            tbl.setModel(dt);
        } catch (Exception ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void WindowLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.put("Button.showMnemonics", Boolean.TRUE);
            /** Set foreground color of combo box when it is in disabled status */
            UIManager.put("ComboBox.disabledForeground", new java.awt.Color(0, 0, 0));
            /** Set background color of combo box when it is in disabled status */
            UIManager.put("ComboBox.disabledBackground", new java.awt.Color(255, 255, 255));

        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
    }

    public static String splitString(String str, String split) {
        String s = str.split(split)[1].toString();
        return s;
    }

    public static String ConvertDateToString(Date d) {
        String s = "";
        try {
            DateFormat formatter;
            formatter = new SimpleDateFormat("MM/dd/yyyy");
            s = formatter.format(d);
            // System.out.println("Today is " + s);
        } catch (Exception e) {
            System.out.println("Exception :" + e);
        }
        return s;
    }

    public static boolean isValidDate(String inDate) {

        if (inDate == null) {
            return false;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        if (inDate.trim().length() != dateFormat.toPattern().length()) {
            return false;
        }
        dateFormat.setLenient(false);
        try {
            dateFormat.parse(inDate.trim());
        } catch (ParseException pe) {
            return false;
        }
        return true;
    }
}
